package com.sakura.mall.coupon.coupon.controller;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.common.response.R;
import com.sakura.mall.coupon.coupon.domain.request.AddCouponRequest;
import com.sakura.mall.coupon.coupon.domain.request.UpdateCouponRequest;
import com.sakura.mall.coupon.coupon.domain.request.CouponPageRequest;
import com.sakura.mall.coupon.coupon.service.CouponService;
import com.sakura.mall.coupon.coupon.domain.vo.CouponVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券信息
 *
 * @author sakura
 */
@RestController
@RequestMapping("/coupon")
@RequiredArgsConstructor
public class CouponController {

    private final CouponService couponService;


    /**
     * 新建
     *
     * @return R {@link Void}
     */
    @PostMapping
    public R<Void> create(@RequestBody @Validated AddCouponRequest request) {
        couponService.add(request);
        return R.ok();
    }


    /**
     * 分页获取
     *
     * @return R {@link Page<CouponVo>}
     */
    @GetMapping("list")
    public R<Page<CouponVo>> page(CouponPageRequest request) {
        Page<CouponVo> ret = couponService.queryPage(request);
        return R.ok(ret);
    }


    /**
     * 根据id删除
     *
     * @return R {@link Void}
     */
    @DeleteMapping("{id}")
    public R<Void> remove(@PathVariable Long id) {
        couponService.removeById(id);
        return R.ok();
    }


    /**
     * 根据id更新
     *
     * @return R {@link Void}
     */
    @PostMapping("{id}")
    public R<Void> update(@PathVariable Long id, @RequestBody UpdateCouponRequest request) {
        couponService.updateById(id, request);
        return R.ok();
    }

}
